//
//  DDStatuesViewModel.swift
//  swift微博
//
//  Created by whitcast on 16/7/14.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit

class DDStatuesViewModel: NSObject {
    //模型
    var statuesModel: DDStatusesModel?
    //coentent的model
    var retweeted_statusModel: DDRetweeted_statusModel?
    //数据来源
    var sources: String?{
        return getSourse()
    }
    //会员等级图片
    var imgOfMB: UIImage?{
        return changePicOfMB((statuesModel?.user?.mbrank) ?? 0)
    }
    
    //认证会员图片
    var verified_typeImg: UIImage?{
        
        return handleImgOfRenZhengMB((statuesModel?.user?.verified_type)!)
    }
    
    //时间标签文字
    var created_atTimeText: String?{
        let date = getTimeFromString((statuesModel?.created_at)!)
        
        let str = handleTimeInterVery(date)
        
        return str        
    }
    
    //初始化,得到模型数据
    init(model: DDStatusesModel) {
        super.init()
        statuesModel = model
        retweeted_statusModel = model.retweeted_status    
    }
    
    //裁剪字符串,得到微博来源
    func getSourse()->String {
        
        let str = statuesModel?.source
        //搜索开始的字符">     //结束的字符</
        if let startIndex = str?.rangeOfString("\">")?.endIndex,let endIndexes = str?.rangeOfString("</")?.startIndex {

        //裁剪字符串
        let wantedStr = str?.substringWithRange(startIndex..<endIndexes)
            
        return wantedStr ?? ""
        }
        return ""
    }
    //根据微博创建时间判断时间标签的显示内容
    
    //获得字符串对应的时间
   private func getTimeFromString(str: String)->NSDate{
        
        let timeFormat = NSDateFormatter()
        
        timeFormat.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        //需要指定花本地信息
        timeFormat.locale = NSLocale(localeIdentifier: "en_US")
        
        let timeFromString = timeFormat.dateFromString(str)!
        
        return timeFromString
        
    }
    //根据时间对象判断时间
  private func handleTimeInterVery(date: NSDate)->String{
        //时间格式
        let timeFormat = NSDateFormatter()

        //是今年
        if isThisYear(date) {
            //是今年
                //今天
        let calender = NSCalendar.currentCalendar()
            //判断是不是今天
            if  calender.isDateInToday(date) {
                //是今天
                //是不是1分钟之内
                let timeInterver = abs(date.timeIntervalSinceNow)//取绝对值
                if timeInterver < 60 {
                      // 1分钟内
                    return "刚刚"
                }else {
                    //不是一分钟之内
                    //是不是1小时之内
                    if timeInterver < 60 * 60 {
                        //是一小时之内
                        let miniteTime = Int(timeInterver / 6.00)
                        let result = Int(miniteTime/10)
                        
                        return "\(result)分钟前"
                    }else{
                        //不是一小时之内
                        let hoursTime = Int(timeInterver / 360.0)
                        
                        let reulst = Int(hoursTime / 10)
                        
                        return "\(reulst)小时前"
                    }
                }
              
            
                //1小时内   xx分钟前
                //  xx小时前
            }else {
                //不是今天
                //是不是昨天
                if calender.isDateInYesterday(date) {
                    //是昨天
                     //昨天    昨天 21:10
                    timeFormat.dateFormat = "HH:mm"
                    return "昨天" + "\(timeFormat.stringFromDate(date))"
                    
                }else{
                    //不是昨天 2016.10.10 21:10
                    timeFormat.dateFormat = "yyyy.MM.dd HH:mm"
                    
                    return timeFormat.stringFromDate(date)
                }
            }

        }else {
            //不是今年
            timeFormat.dateFormat = "yyyy.MM.dd HH:mm"
            //需要指定花本地信息
            timeFormat.locale = NSLocale(localeIdentifier: "en_US")

            return timeFormat.stringFromDate(date)
        }
    }
    //判断是不是今年
    private func isThisYear(date: NSDate)->Bool{
        //年份判断
        let timeFormat = NSDateFormatter()
        
        timeFormat.dateFormat = "yyyy"
        
        let getedDate = timeFormat.stringFromDate(date)
        
        let currentDate = timeFormat.stringFromDate(NSDate())
        
        return getedDate == currentDate
    }
    
    //根据网络内容修改会员等级图片  mBank
    func changePicOfMB(count: Int)-> UIImage {
        if count <= 6 && count >= 1 {
            //图片名字
            let name = "common_icon_membership_level" + "\(count)"
            
            return UIImage(named: name)!
        }else {
            return UIImage(named: "common_icon_membership")!
        }
    }
    
    //判断按钮的显示内容  评论/转发数/赞
    func judgeContentOfBtn(title: String,count: Int)->String{
        if count == 0 {
            //如果数字为0,返回原标题,title为原标题
            return title
        }else if count < 10000{
            return "\(count)"
        }else {
            let num :Int = Int(count)/1000
            
            return "\(Float(num/10))万"
        }
    }
    //暂时的img,用来接收数据
    lazy var img: UIImage = UIImage()
    //根据内容判断认证会员的图片
    //  认证类型等级 认证类型 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
    func handleImgOfRenZhengMB(verified_type: Int)->UIImage{
        
        switch verified_type{
        case 0:
            img = UIImage(named: "avatar_enterprise_vip")!
        case 2,3,5 :
            img = UIImage(named: "avatar_grassroot")!
        case 220:
            img = UIImage(named: "avatar_vgirl")!
        default:
            img = UIImage(named: "avatar_vip")!

        }
        return img
        
    }
    
    
    
    //重写description
    override var description: String{
        
        let  keys = ["retweeted_statusModel"]
        
        return dictionaryWithValuesForKeys(keys).description
    }


}
